<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    include_once '../connection/databaseHandler.php';
    include_once '../connection/sessionHandler.php';
    $database = new DatabaseHandler();
    $session = new SessionHandler();

    $id_photo = $_GET['id'];
    $username = $_GET['tag'];
    $type = $_GET['type'];
    $dbtag = $database->t_foto_tag;
    if($type=="add") {
        $queryCheck = "SELECT id FROM ".$dbtag."
            WHERE ".$dbtag.".id_foto ='$id_photo' AND ".$dbtag.".username = '$username'";
        $resultCheck = $database->execQuery($queryCheck);
        if(mysql_num_rows($resultCheck)==0) {
            $query = "INSERT INTO $dbtag (
                                id_foto,
                                username
                            ) VALUES (
                                '$id_photo',
                                '$username'
                            )";
            if (!$database->execQuery($query)) {
                echo"<script>alert('hai aku salah loch');</script>";
            } else {
                $query = "INSERT INTO ".$database->t_notif."(username,message) VALUE ('".$username."','".$$session->username." tagging a photo of you')";
                $database->execQuery($query);
                header("location:../profile.php?page=photo-detail&photo-id=".$id_photo."&id=".$session->username);
            }
        } else {
            //echo"<script>alert('sudah pernah di tag');</script>";
            header("location:../profile.php?page=photo-detail&photo-id=".$id_photo."&id=".$session->username);
        }
    } else {
        $query = "DELETE FROM $dbtag
        WHERE $dbtag.id_foto = '$id_photo' AND $dbtag.username='$username'";
        if (!$database->execQuery($query)) {
            echo"<script>alert('hai aku salah loch');</script>";
        } else {
            $query = "INSERT INTO ".$database->t_notif."(username,message) VALUE ('".$username."','".$$session->username." remove tag a photo of you')";
            $database->execQuery($query);
            header("location:../profile.php?page=photo-detail&photo-id=".$id_photo."&id=".$session->username);
        }
    }
?>
